// 自定义值
const popup = {
	namespaced: true,
	state: {
		// 自定义的值
		customPopup: null,
		cache: []
	},
	mutations: {
		SET_POPUP(state, arr) {
			let _customPopup = {}
			arr.forEach(item => {
				if (item.type == 'customPopup') {
					_customPopup = item.data;
				}
			})
			state.customPopup = _customPopup;
		},
		REMOVE_POPUP(state) {
			state.customPopup = null;
		},
		SET_CACHE(state, type = 1) {
			if (type == 1) state.cache.push(1);
			else state.cache = [];
		}
	},
	actions: {
		// 设置自定义弹窗
		setPopup({
			commit
		}, arr) {
			commit('SET_POPUP', arr);
		},
		// 删除弹窗配置
		removePopup({
			commit
		}) {
			return new Promise((resolve, reject) => {
				commit('REMOVE_POPUP');
				resolve();
			})
		},
		// 记录连续打开的次数
		setCache({
			commit
		}, type) {
			commit('SET_CACHE', type);
		},
		/**
		 * 打开弹窗参数说明
		 * @param {string} titleText 提示标题，默认没内容就显示提示
		 * @param {string} titleTextColor 标题颜色，默认'#000'
		 * @param {string} contentText 提示内容，默认无
		 * @param {string} contentTextColor 内容颜色，默认'#000'
		 * @param {string} confirmText 确认按钮内容，默认'确认'
		 * @param {Boolean} showClose 是否显示取消按钮，默认true
		 * @param {string} closeText 确认按钮内容，默认'取消'
		 * @param {string} confirmColor 确认按钮颜色，默认'#000'
		 * @param {string} closeColor 取消按钮颜色，默认'#222'
		 * @param {string} url 跳转地址
		 * @param {Fnction} confirm 确认回调
		 * @param {Fnction} cancel 取消回调
		 */
		open({
			commit,
			state
		}, param) {
			console.log('进来了open')
			let {
				titleText,
				titleTextColor,
				contentText,
				contentTextColor,
				closeText,
				closeColor,
				confirmText,
				confirmColor,
				url,
				confirm,
				cancel,
			} = param;
			let data = {
				titleText: titleText ? titleText : '提示',
				titleTextColor: titleTextColor ? titleTextColor : '#000',
				contentText,
				contentTextColor: contentTextColor ? contentTextColor : '#000',
				closeText: closeText ? closeText : '取消',
				closeColor: closeColor ? closeColor : '#000',
				confirmText: confirmText ? confirmText : '确定',
				confirmColor: confirmColor ? confirmColor : '#2979ff',
				url,
				confirm,
				cancel,
				random: new Date().getTime()
			};
			this.dispatch('popup/removePopup').then(() => {
				this.dispatch('popup/setPopup', [{
					type: 'customPopup',
					data: data
				}])
				if (state.cache.length === 0) {
					uni.navigateTo({
						url: '/pages/popup/popup'
					})
				}

			})
		}
	}
};
export default popup;
